package com.ytc.restcontroller;

import com.ytc.mapper.*;
import com.ytc.model.*;
import com.ytc.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ytc.mapper.UserMapper;
import com.ytc.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserServiceImpl {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AuthorMapper authorMapper;
    @Autowired
    private BooklistMapper booklistMapper;
    @Autowired
    private VipMapper vipMapper;


    //柴晓颖：通过用户名查询用户
    @RequestMapping("queryUserByName")
    public User queryUserByName(@RequestParam("uName") String uName){
        return userMapper.queryUserByName(uName);
    }

    /*柴晓颖：新增用户*/
    @RequestMapping("addUser")
    public void addUser(@RequestBody User user){
        String salt = UUID.randomUUID().toString();
        user.setUSalt(salt);
        String Md5 = Md5Util.getMd516(user.getUPwd()+salt);
        String Md532 = Md5Util.getMd532(Md5);
        user.setUPwd(Md532);
        user.setURegisterdate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        user.setUAddress("");
        user.setUMoney(0);
        user.setUBanknum("");
        userMapper.addUser(user);
        System.out.println("新增之后的用户找id" + user);
        if (user.getURoleid() == 3){//如果是成为作者，则新增作者表
            Author author = new Author();
            author.setAName(user.getUName());
            author.setAUid(user.getUId());
            System.out.println("如果是成为作者" + author);
            //并在作者表中新增数据
            authorMapper.addAuthor(author);
        }
        //新增用户关联关系表
        userMapper.addUserRole(user);

    }

    @RequestMapping("queryUserList")
    public List<User> queryUserList(){
        return userMapper.queryUserList();
    }

    /*柴晓颖：修改用户角色id为作者*/
    @RequestMapping("updateRoleId")
    public void updateRoleId(@RequestBody User user,@RequestParam("userResid") Integer userResid){
        userMapper.updateRoleId(user,userResid);
    }

    /**
     * 高媛：根据用户ID查询用户信息
     * @param userid
     * @return
     */
    @RequestMapping("queryUserInfoById")
    public User queryUserInfoById(@RequestParam("userid")Integer userid){
       return userMapper.queryUserInfoById(userid);
    }
    /**
     * 高媛：通过用户ID查找书架
     * @return
     */
    @RequestMapping("queryBookShelfByUserid")
    public List<Booklist> queryBookShelfByUserid(@RequestParam("userid")Integer userid){
        return booklistMapper.queryBookShelfByUserid(userid);
    }
    /**
     * 高媛：通过用户ID查找历史阅读
     * @return
     */
    @RequestMapping("queryBookHistoryByUserid")
    public List<Booklist> queryBookHistoryByUserid(@RequestParam("userid")Integer userid){
        return booklistMapper.queryBookHistoryByUserid(userid);
    }
    /**
     * 高媛：通过用户ID、书籍ID、历史章节ID查询相关内容阅读(连历史记录表)
     */
    @RequestMapping("queryBookByHistory")
    public Booklist queryBookByHistoryOrShelf(@RequestParam("bookid")Integer bookid,@RequestParam("cid") Integer cid,@RequestParam("userid") Integer userid){
        return booklistMapper.queryBookByHistory(bookid,cid,userid);
    }
    /**
     * 高媛：根据书籍ID和用户ID删除历史阅读记录
     */
    @RequestMapping("delBookByHistory")
    public void delBookByHistory(@RequestParam(value = "bookid",required = false,defaultValue = "0")Integer bookid,@RequestParam("userid") Integer userid){
        booklistMapper.delBookByHistory(bookid,userid);
    }
    /**
     * 高媛：通过用户ID、书籍ID、历史章节ID查询相关内容阅读(连书架表)
     */
    @RequestMapping("queryBookByShelf")
    public Booklist queryBookByShelf(@RequestParam("bookid")Integer bookid,@RequestParam("cid") Integer cid,@RequestParam("userid") Integer userid){
        return booklistMapper.queryBookByShelf(bookid,cid,userid);
    }
    /**
     * 高媛：根据书籍ID和用户ID删除历史阅读记录
     */
    @RequestMapping("delBookByShelf")
    public void delBookByShelf(@RequestParam(value = "bookid",required = false,defaultValue = "0")Integer bookid,@RequestParam("userid") Integer userid){
        booklistMapper.delBookByShelf(bookid,userid);
    }
    /**
     * 高媛：批量删除书架书籍
     */
    @RequestMapping("delsBooksByShelf")
    public void delsBooksByShelf(@RequestParam("list")List<Integer> list,@RequestParam("userid") Integer userid){
//        System.out.println(list);
        booklistMapper.delsBooksByShelf(list,userid);
    }
    /**
     * 高媛：批量删除历史阅读
     */
    @RequestMapping("delsBooksByHistory")
    public void delsBooksByHistory(@RequestParam("list")List<Integer> list,@RequestParam("userid") Integer userid){
        booklistMapper.delsBooksByHistory(list,userid);
    }
    /*
    高媛：从历史记录加入书架
     */
    @RequestMapping("historyAddToShelfById")
    public void historyAddToShelfById(@RequestParam("userid") Integer userid,@RequestParam("bookid") Integer bookid){
        booklistMapper.historyAddToShelfById(userid,bookid);
    }
    /**
     * 高媛：批量从历史记录加入书架
     */
    @RequestMapping("HistoryAddsShelfById")
    public void HistoryAddsShelfById(@RequestParam("list")List<Integer> list,@RequestParam("userid") Integer userid){
        booklistMapper.HistoryAddsShelfById(list,userid);
    }

    //张阔：查询用户的角色
    @RequestMapping("isOrNotVip")
    public Integer isOrNotVip(@RequestParam("uId") Integer uId){
        return userMapper.isOrNotVip(uId);
    }

    //张阔：查询vip信息
    @RequestMapping("queryVip")
    public Vip queryVip(@RequestParam("uId") Integer uId){
        return vipMapper.queryVip(uId);
    }

    //张阔：修改用户权限为普通用户
    @RequestMapping("updateRole")
    public void updateRole(@RequestParam("uId") Integer uId,@RequestParam("rId") Integer rId){
        userMapper.updateRole(uId,rId);
    }

    //张阔：把会员过期时间加长
    @RequestMapping("updateDueTimeByMoney")
    public void updateDueTimeByMoney(@RequestParam("money") double money,@RequestParam("uid") Integer uid){
        vipMapper.updateDueTimeByMoney(money,uid);
    }

    //张阔：新增vip
    @RequestMapping("addVipByMoney")
    void addVipByMoney(@RequestParam("money") double money,@RequestParam("uid") Integer uid){
        vipMapper.addVipByMoney(money,uid);
    }


}
